<template>
  <div ref="wrapEl">
    <AdTable @register="registerTable">
      <template #toolbar>
        <Authority :value="'Root.Admin.Wms.MaterialOutStorage.Create'">
          <a-button type="primary" @click="handleCreate">
            <Icon icon="ant-design:plus-circle-outlined" />新增
          </a-button>
        </Authority>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :stop-button-propagation="true"
            :actions="[
              // {
              //   icon: 'clarity:note-edit-line',
              //   auth: '', // 权限控制
              //   tooltip: '编辑',
              //   ifShow: (_action) => {
              //     return true;
              //   },
              //   onClick: handleEdit.bind(null, record),
              // },
              {
                icon: 'mdi:close-circle-outline',
                color: 'error',
                tooltip: '撤销出库',
                auth: '', // 权限控制
                ifShow: (_action) => {
                  if (record.Status == OutStorgeStatusEnum.Complete) {
                    return false;
                  }
                  return true;
                },
                popConfirm: {
                  title: '确认撤销此次入库？',
                  confirm: handleDelete.bind(null, record),
                },
              },
              {
                icon: 'material-symbols:send-rounded',
                auth: 'Root.Admin.Wms.MaterialOutStorage.ApplyToRd', // 权限控制
                tooltip: '提交研发审批',
                onClick: handleApplyToRd.bind(null, record),
                ifShow: (_action) => {
                  if (record.Status == OutStorgeStatusEnum.Complete) {
                    return false;
                  }
                  return true;
                },
              },
              {
                icon: 'material-symbols:send-rounded',
                color: 'error',
                auth: 'Root.Admin.Wms.MaterialOutStorage.ApplyRdToComplete', // 权限控制
                tooltip: '审批通过',
                onClick: handleApplyRdToComplete.bind(null, record),
                ifShow: (_action) => {
                  if (record.Status == OutStorgeStatusEnum.Complete) {
                    return false;
                  }
                  return true;
                },
              },
              {
                icon: 'icon-park-outline:back',
                color: 'error',
                auth: 'Root.Admin.Wms.MaterialOutStorage.RdToApply', // 权限控制
                tooltip: '审批驳回',
                onClick: handleRdToApply.bind(null, record),
                ifShow: (_action) => {
                  if (record.Status == OutStorgeStatusEnum.Complete) {
                    return false;
                  }
                  return true;
                },
              },
            ]"
          />
        </template>
      </template>
    </AdTable>

    <MaterialOutStorageDetail @register="registerDetailModal" :reloadFunc="tableMethods.reload" />
  </div>
</template>

<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { Authority } from '@/components/Authority';
  import { AdTable, useAdTable, TableAction } from '@/components/AdTable';
  import { useLoading } from '@/components/Loading';
  import { useMessage } from '@/hooks/web/useMessage';
  import { FormModeEnum } from '@/enums/formEnum';
  import { AjaxResultTypeEnum } from '@/enums/ajaxResultTypeEnum';
  import { useModal } from '@/components/Modal';
  import MaterialOutStorageDetail from './MaterialOutStorageDetail.vue';
  import Icon from '@/components/Icon/Icon.vue';
  import { OutStorgeStatusEnum } from './materialoutstorage.enum';
  import { columns } from './materialoutstorage.data';
  import {
    Read,
    Create,
    Update,
    Delete,
    ApplyToRd,
    ApplyRdToComplete,
    RdToApply,
  } from './materialoutstorage.api';

  export default defineComponent({
    name: 'MaterialOutStoragePage',
    components: {
      Authority,
      AdTable,
      TableAction,
      Icon,
      MaterialOutStorageDetail,
    },
    setup() {
      const { createMessage } = useMessage();
      const wrapEl = ref<ElRef>(null);
      const [openWrapLoading, closeWrapLoading] = useLoading({
        target: wrapEl,
        props: {
          tip: '加载中...',
          absolute: true,
        },
      });

      const [registerTable, tableMethods] = useAdTable({
        // 表标题呈现器
        title: '入库列表',
        // 表格数据接口请求对象
        api: Read,
        // 详情表单抬头
        detailForm: {
          // 表单标识
          formIndex: 'wms_materialoutstorage_detail',
          // 主键字段
          keyField: 'Id',
          title: '入库详情',
          createApi: Create,
          updateApi: Update,
          deleteApi: Delete,
        },
        // 表单列信息 BasicColumn[]
        columns,
        // 是否可拖拽列
        canColDrag: true,
        // 使用搜索表单
        useSearchForm: true,
        // 显示表格设置工具
        showTableSetting: true,
        //表格设置项
        tableSetting: { redo: true, size: true, setting: true, fullScreen: true },
        // 是否显示表格边框
        bordered: false,
        // 是否显示序号列
        showIndexColumn: true,
        // 表格右侧操作列配置 BasicColumn
        actionColumn: {
          width: 180,
          title: '操作',
          dataIndex: 'action',
          fixed: 'right',
        },
      });

      const [registerDetailModal, detailModalMethods] = useModal();

      async function handleCreate() {
        detailModalMethods.openModal(true, {
          formMode: FormModeEnum.Add,
          record: null,
        });
        //openDetailForm(FormModeEnum.Add);
      }

      async function handleEdit(record: Recordable) {
        detailModalMethods.openModal(true, {
          formMode: FormModeEnum.Edit,
          record: record,
        });
      }

      /** 处理删除 */
      async function handleDelete(record: Recordable) {
        const ajaxResult = await Delete(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          tableMethods.reload();
        }
      }

      async function handleApplyToRd(record: Recordable) {
        const ajaxResult = await ApplyToRd(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          tableMethods.reload();
        }
      }

      async function handleApplyRdToComplete(record: Recordable) {
        const ajaxResult = await ApplyRdToComplete(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          tableMethods.reload();
        }
      }

      async function handleRdToApply(record: Recordable) {
        const ajaxResult = await RdToApply(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          tableMethods.reload();
        }
      }

      return {
        wrapEl,
        registerTable,
        handleCreate,
        handleEdit,
        handleDelete,
        registerDetailModal,
        tableMethods,
        handleApplyToRd,
        handleApplyRdToComplete,
        handleRdToApply,
        OutStorgeStatusEnum,
      };
    },
  });
</script>
